package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrdersettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Service
@Slf4j
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    //任务四方法
    @Transactional
    @Override
    public void upload(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
            log.info("[全部表单],sheets:{}", sheets);
            Sheet sheet = sheets.getSheetAt(0);
            log.info("[单个表单],sheet:{}", sheet);
            List<OrderSetting> insertOrderSettingList = new ArrayList<>();
            List<OrderSetting> updateOrderSettingList = new ArrayList<>();

            for (int i = 1; i <= sheet.getLastRowNum(); i++) {

                OrderSetting orderSetting = new OrderSetting();
                Row row = sheet.getRow(i);
                Date orderDate = row.getCell(0).getDateCellValue();
                log.info("[单行],orderDate:{}", orderDate);

                Double doubleNumber = row.getCell(1).getNumericCellValue();
                String s = String.valueOf(doubleNumber);
                String substring = s.substring(0, s.lastIndexOf("."));
                Integer number = Integer.parseInt(substring);

                log.info("[单行],orderDate:{},number:{}", orderDate, number);
                orderSetting.setOrderDate(orderDate);
                orderSetting.setNumber(number);
                log.info("[保存单行],orderSetting:{}", orderSetting);

                //根据判断是否已存在数据库,存入不同集合
                if (orderSettingDao.selcetByOrderDate(orderDate)!= null){
                    updateOrderSettingList.add(orderSetting);
                } else{
                    insertOrderSettingList.add(orderSetting);
                }
            }
            //执行批量设置和修改:
            log.info("[保存列表],orderSetting:{}", insertOrderSettingList);
            log.info("[修改列表],orderSetting:{}", updateOrderSettingList);
            if (!insertOrderSettingList.isEmpty()){
                orderSettingDao.insertAll(insertOrderSettingList);
            }
            if (!updateOrderSettingList.isEmpty()){
                orderSettingDao.updateAll(updateOrderSettingList);
            }
        } catch (
                IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public List<OrderSetting> getOrderSettingByMonth(int year, int month) {
        // 日期范围
        String startDate = String.format("%d-%d-1",year,month);
        String endDate = String.format("%d-%d-31",year,month);
        return orderSettingDao.selectByOrderDateRange(startDate,endDate);
    }

    @Transactional
    @Override
    public void editNumberByDate(Date orderDate, int number) {
        log.info("[预约设置,根据日期编辑]date:{}", orderDate, number);
        // 判断是否存在
        OrderSetting po = orderSettingDao.selcetByOrderDate(orderDate);
        if (null != po) {
            // 存在,更新
            orderSettingDao.updateNumberById(po.getId(), number);
        } else {
            // 不存在,插入
            orderSettingDao.insert(new OrderSetting(orderDate, number));
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String year, String month) {
        //取出数据库中的数据
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(year, month);
        List<Map> mapList = new ArrayList<>();
        if (orderSettingList!=null||orderSettingList.size()>0) {
            for (OrderSetting orderSetting : orderSettingList) {
                Map map = new HashMap();
                //获得日期
                map.put("date", orderSetting.getOrderDate().getDate());
                //可预约人数
                map.put("number", orderSetting.getNumber());
                //已预约人数
                map.put("reservations", orderSetting.getReservations());
                mapList.add(map);
            }
        }
        return mapList;

    }

    @Override
    public OrderSetting findByDate(Date orderDate) {
        return orderSettingDao.selcetByOrderDate(orderDate);
    }

    @Override
    public void updateReservations(OrderSetting orderSetting) {
        orderSettingDao.updateReservations(orderSetting);
    }
}
